package com.forum.controller;

import cn.undraw.util.result.R;
import com.forum.dao.LikesDao;
import com.forum.entity.Result1;
import com.forum.service.LikesService;
import com.google.common.base.Preconditions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/likes")
@Api(tags = "点赞收藏关注")
public class LikesController {
    @Resource
    private LikesService likesService;

    @Resource
    private LikesDao likesDao;

    /**
     * 用户点赞
     */
    @PostMapping("/doLike")
    @ApiOperation(value = "点赞", notes = "传一个文章id，会更改active")
    public R doLikes(Integer noteId) {
        try {
            // 成功的逻辑
            Preconditions.checkNotNull(noteId, "文章Id不能为空");
//            Boolean res = likesService.dolikes(likes.getNoteId());
            return likesService.dolikes(noteId);
//            return R.error("操作失败！");
        } catch (Exception e) {
            // 业务层如果有抛异常，那么走这里
            log.error("likesController.doLikes.error:{}", e.getMessage(), e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 收藏
     */
    @PostMapping("/doCollection")
    @ApiOperation(value = "收藏", notes = "给一个文章id即可，会更改active")
    public R doCollection(Integer noteId) {
        try {
            Preconditions.checkNotNull(noteId, "文章ID不能为空");
            return likesService.doCollection(noteId);
//            return Result1.error("操作失败！");
        } catch (Exception e) {
            log.error("likesController.doCollection.error:{}", e.getMessage(), e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 关注
     */
    @PostMapping("/doConcern")
    @ApiOperation("关注/取关")
    public Result1 doConcern(Integer bgzId) {
        Result1 doConcern = likesService.doConcern(bgzId);
        return Result1.success(doConcern);
    }

    /**
     * 根据笔记id查询点赞数
     */
    @GetMapping("/getLikeByNoteId")
    public Result1 getLikeByNoteId(Integer noteId){
        return Result1.success(likesDao.getLikes(noteId,1));
    }

    /**
     * 根据笔记id查询收藏数
     */
    @GetMapping("/getCollectionsByNoteId")
    public Result1 getCollectionsByNoteId(Integer noteId){
        return Result1.success(likesDao.getCollections(noteId,1));
    }
}